{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "extra-mozambique",
   "metadata": {},
   "source": [
    "# New Zealand Bank Account Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "permanent-portrait",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "completed-casino",
   "metadata": {},
   "source": [
    "The function `clean_nz_bankaccount()` cleans a column containing New Zealand bank account number (bankaccount) strings, and standardizes them in a given format. The function `validate_nz_bankaccount()` validates either a single bankaccount strings, a column of bankaccount strings or a DataFrame of bankaccount strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "israeli-dryer",
   "metadata": {},
   "source": [
    "bankaccount strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"0102420100194000\"\n",
    "* `standard`: bankaccount strings with proper whitespace in the proper places, like \"01-0242-0100194-000\"\n",
    "* `info`: return a dictionary of data about the supplied number, like {'bank': 'ANZ Bank New Zealand', 'branch': 'ANZ Retail'}. This typically returns the name of the bank and branch and a BIC if it is valid.\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_nz_bankaccount()` and `validate_nz_bankaccount()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "comprehensive-association",
   "metadata": {},
   "source": [
    "### An example dataset containing bankaccount strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sporting-official",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"bankaccount\": [\n",
    "            \"0102420100194000\",\n",
    "            \"01-0242-0100195-00\",\n",
    "            \"999 999 999\",\n",
    "            \"004085616\",\n",
    "            \"002 724 334\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "individual-holder",
   "metadata": {},
   "source": [
    "## 1. Default `clean_nz_bankaccount`\n",
    "\n",
    "By default, `clean_nz_bankaccount` will clean bankaccount strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fourth-bullet",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_nz_bankaccount\n",
    "clean_nz_bankaccount(df, column = \"bankaccount\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "wicked-marker",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "residential-electricity",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "charged-lawyer",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cubic-cedar",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_nz_bankaccount(df, column = \"bankaccount\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "designed-miracle",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "charged-effects",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_nz_bankaccount(df, column = \"bankaccount\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "opposed-operator",
   "metadata": {},
   "source": [
    "### `info`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "extensive-tradition",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_nz_bankaccount(df, column = \"bankaccount\", output_format=\"info\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "baking-chapel",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned bankaccount strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "confident-shoot",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_nz_bankaccount(df, column=\"bankaccount\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "joined-journal",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "current-jungle",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "classified-debut",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_nz_bankaccount(df, \"bankaccount\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "expired-traveler",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "needed-visitor",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_nz_bankaccount(df, \"bankaccount\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "signed-spread",
   "metadata": {},
   "source": [
    "## 4. `validate_nz_bankaccount()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "touched-september",
   "metadata": {},
   "source": [
    "`validate_nz_bankaccount()` returns `True` when the input is a valid bankaccount. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_nz_bankaccount()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_nz_bankaccount()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_nz_bankaccount()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "integrated-dallas",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_nz_bankaccount\n",
    "print(validate_nz_bankaccount(\"0102420100194000\"))\n",
    "print(validate_nz_bankaccount(\"01-0242-0100195-00\"))\n",
    "print(validate_nz_bankaccount(\"999 999 999\"))\n",
    "print(validate_nz_bankaccount(\"51824753556\"))\n",
    "print(validate_nz_bankaccount(\"004085616\"))\n",
    "print(validate_nz_bankaccount(\"hello\"))\n",
    "print(validate_nz_bankaccount(np.nan))\n",
    "print(validate_nz_bankaccount(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "logical-berkeley",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "behind-marine",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_nz_bankaccount(df[\"bankaccount\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "loose-basin",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "billion-equilibrium",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_nz_bankaccount(df, column=\"bankaccount\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cardiovascular-bracket",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ruled-cargo",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_nz_bankaccount(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "still-explanation",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
